// @author Tjzlo
// @version 2025/3/7 20:44

package com.old.service.impl;

import com.old.entity.History;
import com.old.mapper.HistoryMapper;
import com.old.service.HistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class HistoryImpl implements HistoryService {


    private final HistoryMapper historyMapper;

    /**
     * 保存帖子浏览记录
     * @param userId
     * @param id
     */
    @Override
    @Transactional
    public void savePostHistory(Integer userId, Integer id) {
        History history = historyMapper.checkPostHistoryExist(userId, id);
        System.out.println("     ");
        System.out.println("执行历史记录插入,用户:"+userId+",帖子id:"+id);
        System.out.println("     ");
        //查是否存在
        if (history == null) {
            //不存在,新增
            history = new History();
            history.setUserId(userId);
            history.setPostId(id);
            history.setBrowse_time(LocalDateTime.now());

            historyMapper.savePostHistory(history);
        }else {
            //存在,更新浏览时间
            history.setBrowse_time(LocalDateTime.now());
            historyMapper.updatePostHistory(history);
        }
    }

    /**
     * 保存视频浏览记录
     * @param userId
     * @param videoId
     */
    @Override
    public void saveVideoHistory(Integer userId, Integer videoId) {
        History history = historyMapper.checkVideoHistoryExist(userId, videoId);
        System.out.println("     ");
        System.out.println("执行历史记录插入,用户:"+userId+",视频id:"+videoId);
        System.out.println("     ");
        //查是否存在
        if (history == null) {
            //不存在,新增
            history = new History();
            history.setUserId(userId);
            history.setVideoId(videoId);
            history.setBrowse_time(LocalDateTime.now());

            historyMapper.saveVideoHistory(history);
        }else {
            //存在,更新浏览时间
            history.setBrowse_time(LocalDateTime.now());
            historyMapper.updateVideoHistory(history);
        }
    }

    @Override
    public List<History> selectList(Integer userId) {
        return historyMapper.selectListPost(userId);
    }
}
